package com.bookrecomm.dao;

import com.bookrecomm.entity.HotKeyword;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 热门关键词数据访问接口
 */
@Repository
public interface HotKeywordRepository extends JpaRepository<HotKeyword, Long> {
    
    /**
     * 根据分类ID和时间范围查询热门关键词
     * @param categoryId 分类ID，如果为null则查询所有分类
     * @param timeRange 时间范围（day, week, month, year）
     * @param startDate 开始日期
     * @param endDate 结束日期
     * @param limit 限制返回记录数
     * @return 热门关键词列表
     */
    @Query("SELECT hk FROM HotKeyword hk " +
           "WHERE (:categoryId IS NULL OR hk.category.id = :categoryId) " +
           "AND hk.timeRange = :timeRange " +
           "AND hk.statDate BETWEEN :startDate AND :endDate " +
           "ORDER BY hk.weight DESC")
    List<HotKeyword> findTopByCategory(
            @Param("categoryId") Long categoryId,
            @Param("timeRange") String timeRange,
            @Param("startDate") LocalDateTime startDate,
            @Param("endDate") LocalDateTime endDate,
            org.springframework.data.domain.Pageable pageable);
}
